/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jdbcdaos.ProducerDao;
import jdbcpojos.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
 * Akcje zwiazane z zarzadzaniem uzytkownikiem
 *
 * @author piotrek
 */
@Controller
@RequestMapping(value="/jdbc")
public class ProducerController {

	private ProducerDao producerDao;

	@Autowired
    public ProducerController( ProducerDao producerDao) {
		this.producerDao = producerDao;
	}

	@RequestMapping(value="/producer/list")
    protected String showProducerListJdbc(Model model) throws Exception {

		List<Producer> producerList = producerDao.getProducerList();

		model.addAttribute( "producerList", producerList );

		model.addAttribute( "requestType", "jdbc" );

		return "producerList";

	}

	@RequestMapping(value="/{id}/producer", method=RequestMethod.GET)
    protected String showProducerJdbc( @PathVariable("id") String id,
            Model model) throws Exception {
		
		model.addAttribute( "id", id );

		Producer producer = producerDao.getProducer( Integer.parseInt( id ) );

		model.addAttribute( "user", producer );

		return "producer";
    }

	@RequestMapping(value="/{id}/producer/update", method=RequestMethod.GET)
    protected String updateProducerJdbc( @PathVariable("id") String id,
            Model model) throws Exception {

		Producer producer = producerDao.getProducer( Integer.parseInt(id) );

		producer.setName("qwerty");

		producerDao.updateProducer( producer );

		return "success";
    }


	@RequestMapping(value="/{id}/producer/delete", method=RequestMethod.GET)
    protected String deleteProducerJdbc( @PathVariable("id") int id,
            Model model) throws Exception {

		producerDao.deleteProducer(id);

		return "success";
	}


	@RequestMapping(value="/producer/add")
	protected String saveProducer(HttpServletRequest request,
            HttpServletResponse response){

		Producer p = new Producer();
		p.setName( (String)request.getParameter("name") );
		p.setDescription( (String)request.getParameter("description") );

		producerDao.addProducer( p );

		return "success";

	}

}